import { Message } from 'element-ui'
import store from '@/store'

let localConfig = {
    api: '/api'
}
let remoteConfig = {
    api: 'http://www.lxxxy.cn/taggingApi/public/index.php'
}

let imgUrlQ = process.env.NODE_ENV === 'development' ? "http://localhost/taggingApi/public" : "http://www.lxxxy.cn/taggingApi/public/";
imgUrlQ = ""; //使用阿里云oss，无服务器前缀

let nowConfig = process.env.NODE_ENV === 'development' ? localConfig : remoteConfig;

let handleUrl = (originUrl) => {
    originUrl = `${nowConfig.api}${originUrl}`
    return originUrl;
}

export { nowConfig as apiConfig, imgUrlQ }

export function adminPostJson(url, data, success, showMsg = false) {
    url = handleUrl(url)
    if (store.getters['user/getUserStoreType']) {
        if (typeof(success) == 'function') {
            let userMsg = store.getters['user/getUserMsg'];
            let headers = {
                atoken: userMsg.token || '',
                aid: userMsg.objid || '',
                'Content-Type': 'application/json;charset=utf-8'
            }
            fetch(url, { method: 'post', body: JSON.stringify(data), headers: headers })
                .then((res) => { return res.json() })
                .then((response) => {
                    if (showMsg) {
                        if (response.msg) {
                            if (response.code == 200) {
                                Message.success(response.msg);
                            } else if (response.code < 0) {
                                Message.error(response.msg);
                            } else {
                                Message(response.msg)
                            }
                        }
                    }
                    success(response)
                })
                .catch((error) => {
                    Message.error({ message: error });
                    window.console.error(error);
                })
        } else {
            Message.error({ message: 'callback is not a function' })
            window.console.error('callback is not a function')
        }
    } else {
        Message.error({ message: '你禁用了Cookie，且localStorage不能使用，无法发送请求' })
        window.console.error('你禁用了Cookie，且localStorage不能使用，无法发送请求')
    }
}

export function userPostJson(url, data, success, showMsg = false) {
    url = handleUrl(url)
    if (store.getters['user/getUserStoreType']) {
        if (typeof(success) == 'function') {
            let userMsg = store.getters['user/getUserMsg'];
            let headers = {
                atoken: userMsg.token || '',
                aid: userMsg.objid || '',
                'Content-Type': 'application/json;charset=utf-8'
            }
            fetch(url, { method: 'post', body: JSON.stringify(data), headers: headers })
                .then((res) => { return res.json() })
                .then((response) => {
                    if (showMsg) {
                        if (response.msg) {
                            if (response.code == 200) {
                                Message.success(response.msg);
                            } else if (response.code < 0) {
                                Message.error(response.msg);
                            } else {
                                Message(response.msg)
                            }
                        }
                    }
                    success(response)
                })
                .catch((error) => {
                    Message.error({ message: error });
                    window.console.error(error);
                })
        } else {
            Message.error({ message: 'callback is not a function' })
            window.console.error('callback is not a function')
        }
    } else {
        Message.error({ message: '你禁用了Cookie，且localStorage不能使用，无法发送请求' })
        window.console.error('你禁用了Cookie，且localStorage不能使用，无法发送请求')
    }
}